using System;
//linq表达式
using System.Linq;
//Mvc架构
using Microsoft.AspNetCore.Mvc;
//自定义的实体类
using MyApi.Entity;
using MyApi.ParamModel;
using Newtonsoft.Json;
using MyApi.Repository;
using Microsoft.Extensions.Configuration;

namespace MyApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        //定义一个私有字段
        private IRepository<Users> _usersRepository;

        //令牌参数
        private TokenParameter _tokenParameter;
        //依赖注入
        private readonly IConfiguration _configuration;
        // 构造函数
        public UsersController(IConfiguration configuration, IRepository<Users> usersRepository)
        {
            _configuration = configuration;
            _usersRepository = usersRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //获取用户列表
        [HttpGet]
        public dynamic Get()
        {
            //查询所有用户
            var users = _usersRepository.Table.ToList();
            return new
            {
                Code = 1000,
                Data = users,
                Msg = "获取用户列表成功"
            };
        }

        //获取指定Id用户
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            //获取指定Id
            var user = _usersRepository.GetById(id);
            if (user != null)
            {
                return user;
            }
            else
            {
                return new
                {
                    Code = 400,
                    Data = "",
                    Msg = string.Format("你查询的{0}用户不存在！", id)
                };
            }
        }

        //增加用户
        [HttpPost]
        public dynamic Post(NewUser model)
        {
            // Json序列化设置
            JsonSerializerSettings settings = new JsonSerializerSettings();
            // 设置中的引用循环处理设置为忽略
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            //获取用户账号和密码
            var user = new Users
            {
                Username = model.Username,
                Password = model.Password
            };
            //往数据库中插入信息
            _usersRepository.Insert(user);

            //提示信息
            var res = new
            {
                Code = 200,
                Data = user,
                Msg = string.Format("数据插入成功！")
            };
            // 将Json转换序列化对象后的信息返回
            return JsonConvert.SerializeObject(res, settings);
        }
        //修改
        [HttpPut("{id}")]
        public dynamic Put(int id, NewUser model)
        {
            //找到要修改的Id用户
            var user = _usersRepository.GetById(id);
            if (user != null)
            {
                //修改后的用户名赋值给数据库中的用户名
                user.Username = model.Username;
                user.Password = model.Password;
                user.UpdatedTime = DateTime.Now;
                //执行更新操作
                _usersRepository.Update(user);
                return new
                {
                    Code = 200,
                    Data = user,
                    Msg = string.Format("用户{0}信息修改成功！", id)
                };
            }
            else
            {
                return new
                {
                    Code = 400,
                    Msg = string.Format("用户{0}不存在，修改失败！")
                };
            }
        }
        //删除
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            //获取要删除的用户Id
            var user = _usersRepository.GetById(id);
            if (user != null)
            {
                _usersRepository.Delete(id);
                return new
                {
                    Code = 200,
                    Msg = string.Format("用户信息已成功删除！", id)
                };
            }
            else
            {
                return new
                {
                    Code = 400,
                    Msg = string.Format("用户{0}不存在，删除失败！", id)
                };
            }

        }
    }
}

